//
// Created by zhupi on 2024/10/18.
//

#ifndef DEMO2_GEOMETRY_TABLE_WIDGET_H
#define DEMO2_GEOMETRY_TABLE_WIDGET_H

#include <QApplication>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QComboBox>
#include <QPushButton>
#include <QLineEdit>
#include <QCheckBox>
#include <QColorDialog>
#include <QVBoxLayout>
#include <QStyledItemDelegate>
#include <QInputDialog>
#include <QHeaderView>


#include <QVector>
#include <QVariant>
#include "common_cell_struct.h"
#include <QMessageBox>
#include "base_table_widget.h"

class CustomDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
    using QStyledItemDelegate::QStyledItemDelegate;

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const override {
        // 获取单元格中的数据类型
        QVariant dataType = index.data(Qt::UserRole); // 假设使用UserRole存储数据类型信息
        if (dataType.isValid() && dataType.canConvert<int>()) {
            // 如果数据类型是"ComboBox"，则创建一个下拉框编辑器
            CellType editorType = static_cast<CellType>(dataType.toInt());

            // 根据枚举类型创建对应的编辑器
            switch (editorType) {
                case CellType::MaterialCombox:
                {
                    QComboBox *comboBox = new QComboBox(parent);
                    comboBox->addItems({"Option 1", "Option 2", "Option 3"});
                    // 获取指定的显示选项
                    QVariant displayValue = index.data(Qt::DisplayRole);
                    return comboBox;
                }
                case CellType::GroupCombox:{
                    QComboBox *comboBox = new QComboBox(parent);
                    comboBox->addItems({"Option 1", "Option 2", "Option 3"});
                    // 获取指定的显示选项
                    QVariant displayValue = index.data(Qt::DisplayRole);
                    return comboBox;
                }
                case CellType::OrientationCombox:{
                    QComboBox *comboBox = new QComboBox(parent);
                    comboBox->addItems({"Option 1", "Option 2", "Option 3"});
                    // 获取指定的显示选项
                    QVariant displayValue = index.data(Qt::DisplayRole);
                    return comboBox;
                };
                default:
                    // 默认返回文本编辑器
                    return new QLineEdit(parent);
            }
        }
        // 默认行为
        return QStyledItemDelegate::createEditor(parent, {}, index);
    }

    // 将编辑器中的值写回单元格
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override {
        QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
        if (comboBox != nullptr) {
            model->setData(index, comboBox->currentText(), Qt::DisplayRole); // 将选择结果写回单元格
        } else {
            QStyledItemDelegate::setModelData(editor, model, index);
        }
    }
};

class PropertiesWidget : public QWidget {
Q_OBJECT

public:
    void InitTable();

    // 设置默认行为，比如可编辑，
    void SetDefault();
    explicit PropertiesWidget(QWidget *parent = nullptr) : QWidget(parent) {
        // 创建 QTableWidget
        InitTable();

        // -----------------------test------------------------------------
//        QVector<CellWidgetStruct> vec { {"name",CellType::TextDisEditable}
//        , {"name",CellType::TextEditable}
//                , {"none",CellType::TextDisEditable}
//                , {"material combox",CellType::MaterialCombox}
//                , {"color",CellType::Color}
//                , {"Solve Inside",CellType::SolveInsideStatus}
//                , {"Solve Inside",CellType::GroupCombox}
//                , {"Solve Inside",CellType::OrientationCombox}
//        };
//
//        InitTest(1, vec);
        //
        SetDefault();
//        table_->InitConnect();
    }

    void InitTest(int row, const QVector<CellWidgetStruct> &vec);
    void SetCellWidet(int row, int column, const CellWidgetStruct &data);

    void onCellChanged(int row, int column, QTableWidget *tableWidget) {
        // 获取改变后的数据
        QString newValue = tableWidget->item(row, column)->text();
//        QMessageBox::information(nullptr, "Cell Changed", QString("New data at (%1, %2): %3").arg(row).arg(column).arg(newValue));
    }


private:
    MyTableWidget *table_ {nullptr};
//    ComboBoxDelegate *comboBoxDelegate_ {nullptr}; // 代理
};



#endif //DEMO2_GEOMETRY_TABLE_WIDGET_H
